package com.lyj.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lyj.model.Admin;
import com.lyj.model.Option;
import com.lyj.model.Question;
import com.lyj.model.Questionnaire;
import com.lyj.model.Record;
import com.lyj.model.User;
import com.lyj.utils.DateUtil;

/**
 * 
 * @author 李艺江
 * @version 创建时间：2019年5月7日 下午8:00:41
 */

@Controller("adminAction")
@Scope("prototype")
public class AdminAction extends BaseAction<Admin> {

	private static final long serialVersionUID = 1L;

	public String send() {
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		String pageName = (String) hsRequest.getParameter("page");
		return pageName;
	}
	
	public String deleteQuestionnaire() {
		System.out.println(1);
		if (session.get("admin") == null) {
			return "login";
		}
		System.out.println(2);
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		System.out.println(3);
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		questionnaireService.delete(qid);
		request.put("message", "删除成功");
		request.put("questionnaires", questionnaireService.query());
		return "allQuestionnaires";
	}

	public String allQuestionnaires() {
		if (session.get("admin") == null) {
			return "login";
		}
		request.put("questionnaires", questionnaireService.query());
		return "allQuestionnaires";
	}

	public String addQuestionnaire(){
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		String title = (String) hsRequest.getParameter("title");
		Integer surveyTime = Integer.parseInt((String) hsRequest.getParameter("surveyTime"));
		String description = (String) hsRequest.getParameter("description");
		Questionnaire questionnaire = new Questionnaire();
		questionnaire.setTitle(title);
		questionnaire.setDescription(description);
		questionnaire.setSurveyTime(surveyTime);
		questionnaire.setIsUsed(0);
		try {
			questionnaire.setCreateTime(DateUtil.nowToLong());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		questionnaireService.save(questionnaire);
		questionnaire = questionnaireService.getNew(questionnaire);
		request.put("messge", "添加调查成功!");
		request.put("questionnaire", questionnaire);
		return "addQuestionnaire";
	}

	public String allMessages() {
		if (session.get("admin") == null) {
			return "login";
		}
		request.put("allMessages", messageService.query());
		return "allMessages";
	}

	public String allUsers() {
		if (session.get("admin") == null) {
			return "login";
		}
		request.put("allUsers", userService.query());
		return "allUsers";
	}

	public String changePwd() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		String oldPwd = hsRequest.getParameter("oldPwd");
		String newPwd = hsRequest.getParameter("newPwd");
		String newPwd1 = hsRequest.getParameter("newPwd1");
		request.put("oldPwd", oldPwd);
		request.put("newPwd", newPwd);
		request.put("newPwd1", newPwd1);
		if (oldPwd.equals("")) {
			request.put("oldPwdErr", "密码不能为空!");
			return "cp";
		}
		if (newPwd.equals("")) {
			request.put("newPwdErr", "密码不能为空!");
			return "cp";
		}
		if (newPwd1.equals("")) {
			request.put("new1PwdErr", "密码不能为空!");
			return "cp";
		}
		if (!newPwd.equals(newPwd1)) {
			request.put("newPwdErr", "新密码不一致!");
			return "cp";
		} else {
			Admin admin = (Admin) session.get("admin");
			if (!admin.getPassword().equals(oldPwd)) {
				request.put("oldPwdErr", "原密码不正确!");
				return "cp";
			} else {
				admin.setPassword(newPwd);
				adminService.update(admin);
				request.put("message", "更改密码成功!");
				session.put("admin", admin);
				return "cp";
			}
		}
	}

	public String deleteMessage() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer mid = Integer.parseInt((String) hsRequest.getParameter("mid"));
		messageService.delete(mid);
		request.put("allMessages", messageService.query());
		request.put("message", "删除成功!");
		return "allMessages";
	}

	public String changeUser() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer uid = Integer.parseInt((String) hsRequest.getParameter("uid"));
		User user = userService.get(uid);
		request.put("user", user);
		return "changeUser";
	}

	public String updateUser() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer uid = Integer.parseInt((String) hsRequest.getParameter("uid"));
		String name = hsRequest.getParameter("name");
		String pwd = hsRequest.getParameter("pwd");
		User user = userService.get(uid);
		if (name.equals("")) {
			request.put("nameErr", "用户名不能为空!");
			request.put("user", user);
			return "changeUser";
		}
		if (pwd.equals("")) {
			request.put("pwdErr", "密码不能为空!");
			request.put("user", user);
			return "changeUser";
		}
		user.setName(name);
		user.setPassword(pwd);
		userService.update(user);
		request.put("message", "修改成功!");
		request.put("user", user);
		return "changeUser";
	}

	public String deleteUser() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer uid = Integer.parseInt((String) hsRequest.getParameter("uid"));
		userService.delete(uid);
		request.put("message", "删除成功!");
		request.put("allUsers", userService.query());
		return "allUsers";
	}

	public String userMessages() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer uid = Integer.parseInt((String) hsRequest.getParameter("uid"));
		User user = userService.get(uid);
		request.put("userMessages", user.getMessages());
		return "userMessages";
	}

	public String userQuestionnaires() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer uid = Integer.parseInt((String) hsRequest.getParameter("uid"));
		User user = userService.get(uid);
		List<Questionnaire> userQuestionnaires = new ArrayList<Questionnaire>();
		for (Record record : user.getRecords()) {
			userQuestionnaires.add(record.getQuestionnaire());
		}
		HashSet<Questionnaire> hashSet = new HashSet<Questionnaire>(userQuestionnaires);
		userQuestionnaires.clear();
		userQuestionnaires.addAll(hashSet);
		request.put("uid", uid);
		request.put("userQuestionnaires", userQuestionnaires);
		return "userQuestionnaires";
	}

	public String questionnaireDetail() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Questionnaire questionnaire = questionnaireService.get(qid);
		request.put("questionnaire", questionnaire);
		return "questionnaireDetail";
	}

	public String changeQuestionnaire() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Questionnaire questionnaire = questionnaireService.get(qid);
		request.put("questionnaire", questionnaire);
		return "changeQuestionnaire";
	}

	public String updateQuestionnaire() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Questionnaire questionnaire = questionnaireService.get(qid);
		questionnaire.setTitle(hsRequest.getParameter("title"));
		questionnaire.setDescription(hsRequest.getParameter("description"));
		questionnaire.setSurveyTime(Integer.parseInt((String) hsRequest.getParameter("surveyTime")));
		questionnaireService.update(questionnaire);
		request.put("message", "修改成功");
		request.put("questionnaire", questionnaire);
		return "changeQuestionnaire";
	}

	public String startQuestionnaire() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
        Questionnaire questionnaire = questionnaireService.get(qid);
        questionnaire.setIsUsed(1);
        questionnaireService.update(questionnaire);		
        request.put("message", "修改成功");
		request.put("questionnaires", questionnaireService.query());
		return "allQuestionnaires";
	}

	public String shutQuestionnaire() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
        Questionnaire questionnaire = questionnaireService.get(qid);
        questionnaire.setIsUsed(0);
        questionnaireService.update(questionnaire);		
        request.put("message", "修改成功");
		request.put("questionnaires", questionnaireService.query());
		return "allQuestionnaires";
	}
	
	public String addQuestion() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Questionnaire questionnaire = questionnaireService.get(qid);
		String content = hsRequest.getParameter("content");
		Integer type = Integer.parseInt((String) hsRequest.getParameter("type"));
		String optionA = hsRequest.getParameter("optionA");
		String optionB = hsRequest.getParameter("optionB");
		String optionC = hsRequest.getParameter("optionC");
		String optionD = hsRequest.getParameter("optionD");
		
		Question question = new Question();
		question.setContent(content);
		question.setQuestionnaire(questionnaire);
		question.setType(type);
		questionService.save(question);
		
		question = questionService.getNew(question);
		
		Option option1 = new Option();
		Option option2 = new Option();
		Option option3 = new Option();
		Option option4 = new Option();
		
		option1.setQuestion(question);
		option2.setQuestion(question);
		option3.setQuestion(question);
		option4.setQuestion(question);
		
		option1.setContent(optionA);
		optionService.save(option1);
		
		option2.setContent(optionB);
		optionService.save(option2);
		
		option3.setContent(optionC);
		optionService.save(option3);
		
		option4.setContent(optionD);
		optionService.save(option4);
		
		questionnaire = questionnaireService.get(qid);
		request.put("questionnaire", questionnaire);
		request.put("message", "添加成功!");
		return "questionnaireDetail";
	}
	
	public String deleteQuestion() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qnid = Integer.parseInt((String) hsRequest.getParameter("qnid"));
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		questionService.delete(qid);
		Questionnaire questionnaire = questionnaireService.get(qnid);
		request.put("questionnaire", questionnaire);
		request.put("message", "删除成功!");
		return "questionnaireDetail";
		
	}
	
	public String questionsDetail() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Questionnaire questionnaire = questionnaireService.get(qid);
		request.put("questionnaire", questionnaire);
		return "allQuestions";
		
	}
	
	public String questionDetail() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		request.put("question", questionService.get(qid));
		return "questionDetail";
		
	}
	
	public String userAnswers() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Integer uid = Integer.parseInt((String) hsRequest.getParameter("uid"));
		User user = userService.get(uid);
		Questionnaire questionnaire = questionnaireService.get(qid);
		Set<Record> records = user.getRecords();
		ArrayList<Record> userRecords = new ArrayList<Record>();
		for(Record record:records) {
			if(record.getQuestionnaire().getId().equals(questionnaire.getId())) {
				userRecords.add(record);
				System.out.println(1);
			}
		}
		System.out.println(userRecords.size());
		request.put("userRecords", userRecords);
		return "questionAnswers";
	}
	
	public String deleteUserQuestionnaire() {
		if (session.get("admin") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Integer uid = Integer.parseInt((String) hsRequest.getParameter("uid"));
		User user = userService.get(uid);
		Questionnaire questionnaire = questionnaireService.get(qid);
		recordService.deleteByUidQid(user,questionnaire);
		request.put("uid", uid);
		request.put("questionnaires", questionnaireService.query());
		return "userQuestionnaires";
	}
	
}
